package cae.gui.destination;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cae.gui.util.CaeAlert;
import cae.gui.util.CaeJDialog;
import cae.logic.destination.CaeDestination;
import cae.logic.destination.CaeDestinationException;
import cae.logic.destination.CaeDestinatiosManager;


public class CaeAddDestinationDialog extends CaeJDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel panel = null;
	
	private JTextField nick;
	private JTextField machine;
	private JTextField port, chatPort, filePort;
	
	public CaeAddDestinationDialog(JFrame frame, boolean modal)
	{
		
		super(frame, "Agregar destinatario: ", modal);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		panel = new JPanel();
		getContentPane().add(panel);
		
		panel.setLayout( new GridBagLayout() );
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.insets = new Insets(5,5,5,5);
		destConst.anchor = GridBagConstraints.WEST;
		destConst.fill = GridBagConstraints.HORIZONTAL;
		
		destConst.gridx = 0;
		destConst.gridy = 0;
		panel.add(new JLabel("Nick: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 0;
		nick = new JTextField();
		nick.setMinimumSize(new Dimension(200, 22));
		nick.setMaximumSize(new Dimension(200, 22));
		nick.setPreferredSize(new Dimension(200, 22));
		panel.add(nick, destConst);
		
		destConst.gridx = 0;
		destConst.gridy = 1;
		panel.add(new JLabel("Maquina: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 1;
		machine = new JTextField();
		panel.add(machine, destConst);
		
		destConst.gridx = 0;
		destConst.gridy = 2;
		panel.add(new JLabel("Popup Port: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 2;
		port = new JTextField();
		panel.add(port, destConst);
		
		destConst.gridx = 0;
		destConst.gridy = 3;
		panel.add(new JLabel("File Port: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 3;
		filePort = new JTextField();
		panel.add(filePort, destConst);
		
		destConst.gridx = 0;
		destConst.gridy = 4;
		panel.add(new JLabel("Chat Port: "), destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 4;
		chatPort = new JTextField();
		panel.add(chatPort, destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 10;
		JButton add = new JButton("Agregar");
		add.addActionListener(this);
		panel.add(add, destConst);
		
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 4, screenSize.height /4);
        
		setResizable(false);
		setAlwaysOnTop(true);
		setVisible(true);
		
		// TODO FOCO
		//FocusManager.getCurrentManager().
		
	}

	public void actionPerformed(ActionEvent e)
	{
		String nickS = nick.getText();
		String machineS = machine.getText();
		String portS = port.getText();
		String portFileS = filePort.getText();
		String portChatS = chatPort.getText();
		
		// SI NO INGRESO ALGUN DATO
		if( nickS.isEmpty() || machineS.isEmpty() || portS.isEmpty() || portChatS.isEmpty() || portFileS.isEmpty() )
		{
			CaeAlert.error("Ingrese todos los campos requeridos", this);
			return;
		}
		
		// ERROR EN PUERTO POP UP
		int portI = 0;
		try
		{
			portI = Integer.valueOf(portS.trim()).intValue();
			if(portI <= 0)
			{
				throw new NumberFormatException();
			}
		}
		catch(Exception e2)
		{
			CaeAlert.error("Puerto PopUp debe ser un numero", this);
			return;
		}
		
		//	ERROR EN PUERTO FILE
		int portFileI = 0;
		try
		{
			portFileI = Integer.valueOf(portFileS.trim()).intValue();
			if(portFileI <= 0)
			{
				throw new NumberFormatException();
			}
		}
		catch(Exception e2)
		{
			CaeAlert.error("Puerto File debe ser un numero", this);
			return;
		}
		
		// ERROR EN PUERTO POP UP
		int portChatI = 0;
		try
		{
			portChatI = Integer.valueOf(portChatS.trim()).intValue();
			if(portChatI <= 0)
			{
				throw new NumberFormatException();
			}
		}
		catch(Exception e2)
		{
			CaeAlert.error("Puerto Chat debe ser un numero", this);
			return;
		}
		
		CaeDestination dest = new CaeDestination(nickS, machineS, portI, portFileI, portChatI);
		try {
			CaeDestinatiosManager.getInstance().addDestination(dest);
		} catch (CaeDestinationException e1) {
			CaeAlert.error("La maquina ya tiene asociado un usuario", this);
			return;
		}
		
		setVisible(false);
		
	}
}
